<%--
  Created by IntelliJ IDEA.
  User: Gao
  Date: 2023/4/4
  Time: 19:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <%@ include file="header.jsp"%>
</head>
<body>
    <form id="formId" action="<%=request.getContextPath()%>/student?method=add" method="post">
        用户名：<input type="text" name="name"/><br/>
        年龄：<input type="text" name="age"/><br/>
        性别：<input type="text" name="gender"/><br/>
        班级：<select id="selectId" name="banjiId">
             </select>
        <br/>
        <input type="button" onclick="submitForm()" value="添加"/>
    </form>

    <script>
        /*$(function() {
            //页面加载完了
        });*/
        $(function() {
            $.post(
                '${pageContext.request.contextPath}/banji?method=selectAll',
                function(jsonResult) {
                    //{"code":0,"msg":null,"data":[{"id":1,"name":"java1807"},{"id":2,"name":"java1812"},{"id":3,"name":"java230301"}]}
                    console.log(jsonResult);
                    $(jsonResult.data).each(function() {
                        // this :{"id":1,"name":"java1807"}
                        // <option value="1">Java230301</option>
                        $('#selectId').append('<option value="'+this.id+'">'+this.name+'</option>');
                    });
                },
                'json'
            );
        });

        function submitForm() {
            $.post(
                '${pageContext.request.contextPath}/student?method=add',
                $('#formId').serialize(), // {'name':'zhansgan','age':23}
                function(jsonResult) {
                    console.log(jsonResult);
                    if (jsonResult.code == 0) {
                        mylayer.okUrl(jsonResult.msg, '${pageContext.request.contextPath}/student?method=selectByPage')
                    }
                },
                'json'
            );
        }


    </script>
</body>
</html>
